import { View, Button, Input, Text } from '@tarojs/components'
import './index.scss'
import '../../style/button.scss'
import Taro from '@tarojs/taro'
import IconFont from '../../components/iconfont'
import { IP_NAME } from '../../configProvider/namespace'

const Login = () => {
  const login = () => {
    Taro.getUserProfile({
      desc: 'getUserProfile',
      success: (userProfile) => {
        console.log(userProfile)
        Taro.login({
          success(res) {
            if (res.code) {
              console.log(res.code)
              //发起网络请求
              Taro.request({
                method: 'POST',
                url: `${IP_NAME}/wx/login?code=${res.code}`,
                data: {
                  code: res.code
                },
                success: function (res) {
                  console.log('success', res.data)
                  Taro.setStorage({
                    key: "token",
                    data: res.data.data
                  }).then(() => Taro.reLaunch({
                    url: '../index/index'
                  }))
                }
              })
            } else {
              console.log('失败！' + res.errMsg)
            }
          }
        })
      }
    })
  }
  return <>
    <View className="profile-container">
      <View className="profile">
        <IconFont name='user' size={200} color='#999' />
      </View>
    </View>

    <Button className='common-button' onClick={login}>微信一键登陆</Button>
    <Button className='common-button' onClick={() => { Taro.navigateTo({ url: '../register/index' }) }}>没有账号?点击注册</Button>
  </>

}
export default Login;
